/*
 * device.cpp
 *
 *  Created on: Jun 24, 2013
 *      Author: r.leonov
 */

#include "device.h"
#include "led.h"
#include "adc.h"
#include "main.h"
#include "led_indicate.h"

Device_t Tree;
LedSmooth_t LedSmooth;


void Device_t::Task(){
    switch (State) {
        case tsActive:

            if(!Delay.Elapsed(&Timer, TIME_OF_SIL)){
                // Branches.Task();
            }
            else {
                if(!SensorActive()){
                    // Set branches light to 0
                    TreeLed.FirstBranch.SetSmoothly(0);
                    TreeLed.SecondBranch.SetSmoothly(0);
                    Uart.Printf("tsSuspend\r");
                    State = tsSuspend;
                }
//                Uart.Printf("tsActive\r");
            }
            break;

        case tsSleep:
//            StandBy();      // Entering StndBy mode
            break;

        case tsWakeup:
            if(Delay.Elapsed(&Timer, 249)){
                Count++;
                if(SensorActive()){
                    TreeLed.FirstBranch.SetSmoothly(999);
                    TreeLed.SecondBranch.SetSmoothly(999);
                    Uart.Printf("tsActive\r");
                    State = tsActive;
                }
                if(Count == STARTSLEEP_COUNT){
                    Uart.Printf("Nothing left near tsSuspend\r");
                    State = tsSuspend;
                }
            }
            break;

        case tsSuspend:
            if(Delay.Elapsed(&Timer, 249)){
                if ((TreeLed.FirstBranch.GetValue() == 0) and (TreeLed.SecondBranch.GetValue() == 0)){
                    Uart.Printf("tsSleep\r");
                    State = tsSleep;
                } // if branches light off
                else {
                    if(SensorActive()){ // Sensor active again
                        // Need to light on branches again
                        TreeLed.FirstBranch.SetSmoothly(999  );
                        TreeLed.SecondBranch.SetSmoothly(999);
                        Uart.Printf("tsActve2\r");
                        State = tsActive;
                    }
                }
            } // if delay
            break;
    }
}


void Device_t::Init(){
      Count = 0;
//    LedSmooth.Init(GPIOB, 7, TIM4, 1000, 0, 2, false);
//    LedSmooth.Set(600);
}
